<?php
	include_once ("sgbd.php");
	
	$email = 'bourtaillar@free.fr';
// 	$email = 'mguignes@gmail.com';
	$subject = 'Demande d\'inscription';
	
	$data[0]->name = 'lastname';
	$data[0]->value = 'Nom';
	$data[0]->type = 'text';
	
	$data[1]->name = 'firstname';
	$data[1]->value = 'Prenom';
	$data[1]->type = 'text';
	
/*	$data[2]->name = 'age';
	$data[2]->value = 'Age';
	$data[2]->type = 'text';*/
	
	$data[3]->name = 'email';
	$data[3]->value = 'E-mail';
	$data[3]->type = 'text';
	
	$data[4]->name = 'login';
	$data[4]->value = 'Identifiant';
	$data[4]->type = 'text';
	
	$data[5]->name = 'passwd';
	$data[5]->value = 'Mot de passe';
	$data[5]->type = 'password';
	
	$data[6]->name = 'passwd2';
	$data[6]->value = 'Mot de passe (confirmation)';
	$data[6]->type = 'password';
	
	function exists ($login) {				
		$query = "SELECT * FROM USER WHERE login = '$login'";
		$res = query ($query) or die("Erreur sur la requete: ".mysql_error());
		$lines = mysql_num_rows($res);
		
		close ();
		
		return $lines == 1;   
	}
	
	function getError ($_POST) {
		if ($_POST['firstname'] == "")
			return "Entrez votre pr&eacute;nom";
		
		if ($_POST['lastname'] == "")
			return "Entrez votre nom";
	
		if ($_POST['login'] == "")
			return "Entrez un identifiant";
		
		if (preg_match("/.+@.+\..+/", $_POST['email']) == 0)
			return "Entrez une adresse e-mail valide";

		if ($_POST['passwd'] == "")
			return "Entrez un mot de passe";
   
		if ($_POST['passwd'] != $_POST['passwd2'])
			return "Mots de passe non identiques";
		
		if (exists ($_POST['login']))
			return "Identifiant d&eacute;j&agrave; utilis&eacute;";
		
		return false;
	}
		
	$err = getError ($_POST);
	
	if (count($_POST) > 0 && !$err) {		// envoy d'e-mail
			$msg = "La personne suivante souhaiterait s'enregistrer:\n\n\n";
			
			foreach ($data as $d)
				if ($d->name != 'passwd2')
					$msg .= $d->value.": ".$_POST[$d->name]."\n";
						
			$msg .= "\n\n\nCliquez sur le lien suivant pour effectuer l'enregistrement.\n\n";
			$msg .= "http://bourtaillar.free.fr/admin.php?action=adduser";	//."login=$login&passwd=".md5($passwd);
			
			foreach ($data as $d) {
				if ($d->name == 'passwd2')
					continue;
				
				if ($d->name == 'passwd')
					$msg .= '&'.$d->name.'='.md5($_POST[$d->name]);
				else if ($d->name == 'firstname' || $d->name == 'lastname')
					$msg .= '&'.$d->name.'='.urlencode($_POST[$d->name]);
				else
					$msg .= '&'.$d->name.'='.$_POST[$d->name];
			}
			$msg .= "&key=".md5($_POST['login']);
			$msg .= "\n";
			
			$mail = mail($email, $subject, $msg);
			
			if ($mail)
				print ("<html><h3>Envoi r&eacute;ussi ! </h3><br /></html>");
			else
				die("Erreur lors de l'envoie du message");
			
	} else {		
?>

<html>
<head>
	<title>S'ENREGISTRER</title>
</head>
<body>
	<form method='post' action='form.php'>
<?php
		if (count($_POST) > 0)		// print error
			echo $err."<br /><br />";

		foreach ($data as $d)
			echo $d->value.": <input type='".$d->type."' name='".$d->name."' text='".$_POST[$d->name]."' /> <br />";
?>
		<br />
		<input type='submit' value='Envoyer' />
	</form>
</body>	
</html>

<?php
	}
?>

